package cn.appsys.controller;

import cn.appsys.pojo.DevUser;
import cn.appsys.service.devUser.DevUserService;
import cn.appsys.tools.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping( value = "/dev")
public class DevUserController {
    @Resource
    private DevUserService devUserService;
    @RequestMapping(value = "/login")
    public String login(){
        return "devlogin";
    }
    @RequestMapping(value = "dologin")
    public String doLogin(@RequestParam String devCode,
                          @RequestParam String devPassword,
                          HttpSession session,
                          HttpServletRequest request){
        DevUser devUser=null;
        devUser=devUserService.login(devCode,devPassword);
        if(null!=devUser){
            session.setAttribute(Constants.USER_SESSION,devUser);
            return "redirect:/dev/flatform/main";
        }else{
            request.setAttribute("error","用户名或密码错误");
            return "devlogin";
        }
    }
    @RequestMapping(value = "flatform/main")
    public String main(HttpSession session){
        if(session.getAttribute(Constants.USER_SESSION)==null){
            return "redirect:/dev/login";
        }
        return "developer/main";
    }
    @RequestMapping(value = "logOut")
    public String logOut(HttpSession session){
        if(session.getAttribute(Constants.USER_SESSION)!=null){
            session.removeAttribute(Constants.USER_SESSION);
        }
        return "devlogin";
    }
}
